<?php

/**
 * This is the model class for table "announcement_assign".
 *
 * The followings are the available columns in table 'announcement_assign':
 * @property integer $member_id
 * @property integer $announcement_id
 * @property integer $purpose_join
 * @property integer $event_joined
 */
class AnnouncementAssign extends AnnouncementAssignBase
{
	public $count_no_reply;
	public $count_purpose_join;
	public $count_no_join;
	public $count_joined;
	public $count_assign;
	public $count_view;
	/**
	 * Returns the static model of the specified AR class.
	 * @return AnnouncementAssign the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'member' => array(self::BELONGS_TO, 'Member', 'member_id', 'joinType' => 'INNER JOIN'),
			'announcement' => array(self::BELONGS_TO, 'Announcement', 'announcement_id', 'joinType' => 'INNER JOIN'),
			/*'views' => array(self::HAS_ONE, 'AnnouncementView', '',
							 'on' => 't.announcement_id = views.announcement_id and t.member_id = views.user_id',
							 'joinType' => 'LEFT JOIN'),*/
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'member_id' => 'Thành viên',
			'announcement_id' => 'Thông báo',
			'purpose_join' => 'Dự kiến tham dự',
			'event_joined' => 'Đã tham dự',
			'view_fg' => 'Xem',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('member_id',$this->member_id);
		$criteria->compare('announcement_id',$this->announcement_id);
		$criteria->compare('purpose_join',$this->purpose_join);
		$criteria->compare('event_joined',$this->event_joined);
		$criteria->compare('view_fg',$this->view_fg);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function countByAnnouncement($announcement_id)
	{
		$result = array();
		// Count from assign
		$criteria = new CDbCriteria;
		$criteria->with = array('announcement', 'member', 'member.memberCode');
		$criteria->select = 'announcement_id,
							 COUNT(t.id) as count_assign,
							 SUM(case when purpose_join = 0 then 1 else 0 end) as count_no_reply,
							 SUM(case when purpose_join = 1 then 1 else 0 end) as count_purpose_join,
							 SUM(case when purpose_join = 2 then 1 else 0 end) as count_no_join,
							 SUM(case when view_fg = 1 then 1 else 0 end) as count_view,
							 SUM(event_joined) as count_joined';
		if (!is_array($announcement_id) && $announcement_id != "") {
			$criteria->condition = 't.announcement_id = :announcement_id';
			$criteria->params = array(':announcement_id'=> $announcement_id);
		}
		elseif (is_array($announcement_id) && !empty($announcement_id)) {
			$criteria->addInCondition('t.announcement_id', $announcement_id);
			$criteria->group = 'announcement_id';
		}
		$data = $this->model()->findAll($criteria);
		foreach ($data as $eachData) {
			$result[$eachData->announcement_id]["count_no_reply"] 		= $eachData["count_no_reply"];
			$result[$eachData->announcement_id]["count_view"] 			= $eachData["count_view"];
			$result[$eachData->announcement_id]["count_purpose_join"] 	= $eachData["count_purpose_join"];
			$result[$eachData->announcement_id]["count_no_join"] 		= $eachData["count_no_join"];
			$result[$eachData->announcement_id]["count_joined"] 		= $eachData["count_joined"];
			$result[$eachData->announcement_id]["count_assign"] 		= $eachData["count_assign"];
		}
		return $result;
	}

	public function assignAnnouncement($announcement_id)
	{
		$result = array();
		// Count from assign
		$criteria = new CDbCriteria;
		$criteria->with = array('announcement', /*'views', */'member', 'member.memberCode');
		if (!is_array($announcement_id) && $announcement_id != "") {
			$criteria->condition = 't.announcement_id = :announcement_id';
			$criteria->params = array(':announcement_id'=> $announcement_id);
		}
		elseif (is_array($announcement_id) && !empty($announcement_id)) {
			$criteria->addInCondition('t.announcement_id', $announcement_id);
		}
		$criteria->group = 't.id';
		$data = $this->model()->findAll($criteria);
		$i = 0;
		foreach ($data as $eachData) {
			$result[$eachData->announcement_id][$i]["member_id"] 	= $eachData->member_id;
			$result[$eachData->announcement_id][$i]["purpose_join"] = $eachData->purpose_join;
			$result[$eachData->announcement_id][$i]["event_joined"] = $eachData->event_joined;
			$result[$eachData->announcement_id][$i]["comment"]		= $eachData->comment;
			$result[$eachData->announcement_id][$i]["view_fg"] 		= $eachData->view_fg;
			$result[$eachData->announcement_id][$i]["fullname"] 	= $eachData->member->fullname;
			$result[$eachData->announcement_id][$i]["coursename"] 	= $eachData->member->memberCode->course->name;
			$result[$eachData->announcement_id][$i]["code"] 		= $eachData->member->memberCode->code;
			//$result[$eachData->announcement_id][$i]["views"] 		= $eachData->views->user_id;
			$i++;
		}
		return $result;
	}
}